package com.atguigu.customer;

import com.alibaba.fastjson.JSON;
import com.atguigu.client.PaymentFeignClient;
import com.atguigu.constant.MqConst;
import com.atguigu.entity.OrderInfo;
import com.atguigu.enums.OrderStatus;
import com.atguigu.enums.ProcessStatus;
import com.atguigu.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;

@Component
public class OrderCustomer {
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private PaymentFeignClient paymentFeignClient;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(queues ={MqConst.CANCEL_ORDER_QUEUE})
    public void timeOutOrder(Long orderId){
        //a.订单表状态改为关闭状态
        OrderInfo orderInfo = orderInfoService.getById(orderId);
        if(orderInfo!=null) {
            orderInfo.setOrderStatus(OrderStatus.CLOSED.name());
            orderInfo.setProcessStatus(ProcessStatus.CLOSED.name());
            orderInfoService.updateById(orderInfo);
        }
        //b.把支付表状态也改为关闭状态
        String outTradeNo = orderInfo.getOutTradeNo();
        rabbitTemplate.convertAndSend(MqConst.CLOSE_PAYMENT_EXCHANGE,MqConst.CLOSE_PAYMENT_ROUTE_KEY,outTradeNo);

        //c.关闭交易接口
        //c.如果支付宝那边有交易记录也需要关闭
        boolean flag = paymentFeignClient.queryAliPayTrade(orderId);
        if(flag){
            //出现二维码 未支付 可以关闭交易
            paymentFeignClient.closeAliPayTrade(orderId);
        }

    }
    //2.支付成功之后更新订单信息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.PAY_ORDER_QUEUE,durable = "false"),
            exchange = @Exchange(value = MqConst.PAY_ORDER_EXCHANGE,durable = "false",autoDelete = "true"),
            key = {MqConst.PAY_ORDER_ROUTE_KEY}
    ))
    public void updateOrderAfterPaySuccess(Long orderId, Message message , Channel channel) {
       if(!StringUtils.isEmpty(orderId)){
           //查询订单的基本信息，和详情信息,方便以后拆单
           OrderInfo orderInfo=orderInfoService.checkOrderWithDetail(orderId);
           //判断此时的订单是否为未支付
           if(orderId!=null&& ProcessStatus.UNPAID.name().equals(orderInfo.getProcessStatus())){
               //此时需要修改
               orderInfoService.upOrderStatus(orderInfo,ProcessStatus.PAID);

               //通知 库存系统，发送一个减库的通知s
               orderInfoService.sendMsgToWareHouse(orderInfo);
           }
       }
    }
    //3.仓库系统减库存成功之后的代码
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.SUCCESS_DECREASE_STOCK_QUEUE,durable = "false"),
            exchange = @Exchange(value = MqConst.SUCCESS_DECREASE_STOCK_EXCHANGE,durable = "false"),
            key = {MqConst.SUCCESS_DECREASE_STOCK_ROUTE_KEY}
    ))
    public void updateOrderAfterDecraseStock(String jsonData){
        //把json转换为map
        Map<String, Object> map = JSON.parseObject(jsonData);
        String orderId=(String)map.get("orderId");
        String status=(String)map.get("status");
        OrderInfo orderInfo = orderInfoService.checkOrderWithDetail(Long.parseLong(orderId));
        if("DEDUCTED".equals(status)){
            //减库成功了
            orderInfoService.upOrderStatus(orderInfo,ProcessStatus.WAITING_DELEVER);
        }else {
            //减库异常
            orderInfoService.upOrderStatus(orderInfo,ProcessStatus.STOCK_EXCEPTION);
        }
    }
}
